<template>
  <div>

    <el-form ref="form"
             :model="form"
             label-width="80px"
             style="margin-top:20px">
      <el-form-item label="商品名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="商品图片">
        <el-upload class="avatar-uploader"
                   action="http://192.168.11.18:8080/file/uploadImage"
                   :show-file-list="false"
                   :on-success="getUploadFileName">
          <img v-if="imageUrl"
               :src="imageUrl"
               class="avatar">
          <i v-else
             class="el-icon-upload avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="商品类型">
        <el-select v-model="form.type"
                   placeholder="请选择商品类型">
          <el-option label="食品"
                     value="1"></el-option>
          <el-option label="电器"
                     value="2"></el-option>
          <el-option label="日用"
                     value="3"></el-option>
          <el-option label="工具"
                     value="4"></el-option>
          <el-option label="服饰"
                     value="5"></el-option>
          <el-option label="其他"
                     value="6"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品价格">
        <el-input v-model="form.price"></el-input>
      </el-form-item>

      <el-form-item label="联系电话">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>

      <el-form-item label="商品描述">
        <el-input v-model="form.content"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   @click="onSubmit">上传新商品</el-button>

      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import axios from 'axios';
import http from '../../../axios_config'
export default {
  data () {
    return {
      form: {
        name: '',
        image: '',
        type: '',
        price: '',
        phone: '',
        content: '',

      },
      imageUrl: '',
    };
  },
  methods: {
    getUploadFileName (res) {
      this.imageUrl = res;
      this.form.image = res;
      console.log(res)
    },

    onSubmit () {
      //此处编写需要提交的数据，例：
      http.post("http://192.168.11.18:8080/goods/insertGoods", this.form).then(res => {
        if (res.data == true) {
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          //通知父组件关闭对话框
          this.$emit("change(1)")
        } else {
          this.$message({
            showClose: true,
            message: '操作失败，请稍后再试',
            type: 'warning'
          });
        }
      })
    },

  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>